package com.example.demo_text;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@WebServlet("/SubmitAnswerServlet")
public class SubmitAnswerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        int questionId = Integer.parseInt(request.getParameter("question_id"));
        String answerContent = request.getParameter("answer_content");

        AnswerDAO.addAnswer(questionId, answerContent);

        String courseId = request.getParameter("course_id");

        // 修改：重定向回 class_space.jsp 并激活 discussions tab
        response.sendRedirect("class_space.jsp?course_id=" + courseId + "&tab=discussions");
    }


}
